
import { message } from 'antd';
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

import { getUserShowUserUser, getUserIdentity, getApi_authority, getIdentity_api_authority_relation, getView_authority, getIdentity_view_authority_relation, setIdentity, setAuthorityApi, setauthorityView, setsetIdentityApi, setsetIdentityView, setuser, setuseruser,getUserInfo } from '@/services/user';

import { IuserUser, IuserIdenttiy, IuserApi_authority, IuserIdentity_api_authority_relation, IuserView_authority, IuserIdentity_view_authority_relation,IsetUserAuthorityApis } from '@/utils/interface';

let hasUserInfo = false;


export interface UserModelState {
  // userUser: IuserUser[];
  // userIdenttiy: IuserIdenttiy[];
  // userApi_authority: IuserApi_authority[],
  // userIdentity_api_authority_relation: IuserIdentity_api_authority_relation[],
  // userView_authority: IuserView_authority[],
  // userIdentity_view_authority_relation: IuserIdentity_view_authority_relation[],
  // setUserIdentitys: string,
  // setUserAuthorityApis: string,
  setUserauthorityViews: string,
  setUsersetIdentityApi: string,
  setUsersetIdentityView: string,
  setUserusers: string,
  setUseruserusers: string,
    userUser: IuserUser[];
    userIdenttiy: IuserIdenttiy[];
    userApi_authority: IuserApi_authority[],
    userIdentity_api_authority_relation: IuserIdentity_api_authority_relation[],
    userView_authority: IuserView_authority[],
    userIdentity_view_authority_relation: IuserIdentity_view_authority_relation[],
    setUserIdentitys:string,
    setUserAuthorityApis:string,
}

export interface UserModelType {
  namespace: 'user';
  state: UserModelState;
  effects: {
    getUserShowUserUsers: Effect,
    getUserIdentitys: Effect,
    getApi_authoritys: Effect,
    getIdentity_api_authority_relations: Effect,
    getView_authoritys: Effect,
    getIdentity_view_authority_relations: Effect,
    setIdentitys: Effect,
    setAuthorityApis: Effect,
    setauthorityViews: Effect,
    setsetIdentityApis: Effect,
    setsetIdentityViews: Effect,
    setusers: Effect,
    setuserusers: Effect,
    getUserInfo: Effect,
  };
  reducers: {
    save: Reducer<UserModelState>;
  }
  subscriptions: { setup: Subscription };
}

const UserModel: UserModelType = {
  namespace: 'user',

  state: {

    userUser: [],

    userIdenttiy: [],

    userApi_authority: [],

    userIdentity_api_authority_relation: [],

    userView_authority: [],

    userIdentity_view_authority_relation: [],

    setUserIdentitys: '',

    setUserAuthorityApis: '',

    setUserauthorityViews: '',

    setUsersetIdentityApi: '',

    setUsersetIdentityView: '',

    setUserusers: '',

    setUseruserusers: '',

  },

  effects: {
    // 用户数据
    *getUserShowUserUsers({ payload }, { call, put }) {
      let res = yield call(getUserShowUserUser);
      // console.log(res.data);
      res.code == 1 ? yield put({ type: 'save', payload: { userUser: res.data } }) : {};
    },
    // 身份数据
    *getUserIdentitys({ payload }, { call, put }) {
      let res = yield call(getUserIdentity);
      // console.log(res);
      res.code == 1 ? yield put({ type: 'save', payload: { userIdenttiy: res.data } }) : []
    },
    // api接口权限数据
    *getApi_authoritys({ payload }, { call, put }) {
      let res = yield call(getApi_authority);
      // console.log(res);
      res.code == 1 ? yield put({ type: 'save', payload: { userApi_authority: res.data } }) : []
    },
    // 展示身份和api权限关系
    *getIdentity_api_authority_relations({ payload }, { call, put }) {
      let res = yield call(getIdentity_api_authority_relation);
      // console.log(res);
      res.code == 1 ? yield put({ type: 'save', payload: { userIdentity_api_authority_relation: res.data } }) : []
    },
    // 获取视图权限数据
    *getView_authoritys({ payload }, { call, put }) {
      let res = yield call(getView_authority);
      // console.log(res);
      res.code == 1 ? yield put({ type: 'save', payload: { userView_authority: res.data } }) : []
    },
    // 给身份设定视图权限
    *getIdentity_view_authority_relations({ payload }, { call, put }) {
      let res = yield call(getIdentity_view_authority_relation);
      // console.log(res);
      res.code == 1 ? yield put({ type: 'save', payload: { userIdentity_view_authority_relation: res.data } }) : []
    },
    // 添加身份
    *setIdentitys({ payload }, { call, put }) {
      let res = yield call(setIdentity, payload.identity_text);
      if (res.code === 1) {
        (function success() {
          message.success(res.msg);
        }());
      } else if (res.code == 0) {
        (function warning() {
          message.warning(res.msg);
        }());
      } else {
        (function error() {
          message.error(res.msg);
        }());
      }
    },
    // 添加api接口权限
    *setAuthorityApis({ payload }, { call, put }) {
      let res = yield call(setAuthorityApi, payload);
      console.log(res, payload);
      if (res.code === 1) {
        (function success() {
          message.success(res.msg);
        }());
      } else if (res.code == 0) {
        (function warning() {
          message.warning(res.msg);
        }());
      } else {
        (function error() {
          message.error(res.msg);
        }());
      }
    },
    // 添加视图权限
    *setauthorityViews({ payload }, { call, put }) {
      let res = yield call(setauthorityView, payload);
      console.log(res, payload);
      if (res.code === 1) {
        (function success() {
          message.success(res.msg);
        }());
      } else if (res.code == 0) {
        (function warning() {
          message.warning(res.msg);
        }());
      } else {
        (function error() {
          message.error(res.msg);
        }());
      }
    },
    // 给身份设定api接口权限
    *setsetIdentityApis({ payload }, { call, put }) {
      let res = yield call(setsetIdentityApi, payload);
      console.log(res, payload);
      if (res.code === 1) {
        (function success() {
          message.success(res.msg);
        }());
      } else if (res.code == 0) {
        (function warning() {
          message.warning(res.msg);
        }());
      } else {
        (function error() {
          message.error(res.msg);
        }());
      }
    },
    // 给身份设定视图权限
    *setsetIdentityViews({ payload }, { call, put }) {
      let res = yield call(setsetIdentityView, payload);
      console.log(res, payload);
      if (res.code === 1) {
        (function success() {
          message.success(res.msg);
        }());
      } else if (res.code == 0) {
        (function warning() {
          message.warning(res.msg);
        }());
      } else {
        (function error() {
          message.error(res.msg);
        }());
      }
    },
    // 添加用户
    *setusers({ payload }, { call, put }) {
      let res = yield call(setuser, payload.data);
      console.log(res, payload);
      if (res.code === 1) {
        (function success() {
          message.success(res.msg);
        }());
      } else if (res.code == 0) {
        (function warning() {
          message.warning(res.msg);
        }());
      } else {
        (function error() {
          message.error(res.msg);
        }());
      }
    },
    // 更新用户
    *setuserusers({ payload }, { call, put }) {

      let res = yield call(setuseruser, payload);
      console.log(res, payload);
      if (res.code === 1) {
        (function success() {
          message.success(res.msg);
        }());
      } else if (res.code == 0) {
        (function warning() {
          message.warning(res.msg);
        }());
      } else {
        (function error() {
          message.error(res.msg);
        }());
      }
    },
      // 获取当前用户信息
    *getUserInfo({ }, { call, put }) {
      let result = yield call(getUserInfo);
      if (result.code === 1) {
        hasUserInfo = true;
        yield put({
          type: 'save',
          payload: { userInfo: result.data }
        })
      }
    }
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({ type: 'user' })
        }
      });
    },
  },
};

export default UserModel;
